﻿using RimWorld;
using System.Collections.Generic;
using System.Linq;
using Verse;

namespace Miao.Stand
{
    public static class StandGenerator
    {
        public struct StandTuple
        {
            public HediffDef standhediffDef;
            public PawnKindDef standPawnKindDef;

        }
        public static List<StandTuple> standTuples = new List<StandTuple>();

        static StandGenerator()
        {
            var hediffs = GenDefDatabase.DefsToGoInDatabase<HediffDef>(Stand.ModSingleton.Content);
            var pawnKindDefs = GenDefDatabase.DefsToGoInDatabase<PawnKindDef>(Stand.ModSingleton.Content);

            foreach (HediffDef hediffDef in hediffs)
            {

                if (hediffDef.hediffClass == typeof(StandMasterHediff) || hediffDef.hediffClass.IsSubclassOf(typeof(StandMasterHediff)))
                {
                    StandTuple stand = new StandTuple();
                    stand.standhediffDef = hediffDef;
                    string standName = stand.standhediffDef.defName.Replace("Hediff", "");
                    bool success = false;
                    foreach (PawnKindDef pawnKind in pawnKindDefs)
                    {

                        if (pawnKind.defName == standName)
                        {
                            success = true;
                            stand.standPawnKindDef = pawnKind;
                            standTuples.Add(stand);
                            break;
                        }
                    }
                    if(!success) Log.Error($"没能成功找到{hediffDef.defName}的对应替身Race，应该将Race起名为{standName}");
                }

            }
            //var allStandHediff = GenDefDatabase.GetAllDefsInDatabaseForDef(typeof(StandHediff));
            //var def = allStandHediff.RandomElement();

        }


        public static StandMasterHediff StandHediffGenerator(Pawn owner)
        {
            //int randStand = Rand.Range(0, 1);//虽然想用反射来调StandHediffOf但太麻烦了
            //switch (randStand)
            //{
            //    case 0:

            //        StandHediff standHediff = (StandHediff)target.health.AddHediff(StandHediffDefOf.StarPlatinumHediff);
            //        standHediff.CreateStand(target);
            //        return standHediff;
            //}

            StandTuple standTuple = standTuples.RandomElement();
            return StandHediffGenerator(owner, standTuple);
            //HediffMaker.MakeHediff(def, pawn);
        }
        public static StandMasterHediff StandHediffGenerator(Pawn owner,StandTuple standTuple)
        {
            StandMasterHediff standHediff;

            standHediff =  owner.health.hediffSet.GetFirstHediff<StandMasterHediff>();

            if (standHediff == null)
            {
                standHediff = (StandMasterHediff)owner.health.AddHediff(standTuple.standhediffDef);
                standHediff.stand = CreateStandPawn(standTuple.standPawnKindDef, owner);
                //Need_StandPower standPower = owner.needs.TryGetNeed<Need_StandPower>();
            }
            return standHediff;
        }



        public static StandPawn CreateStandPawn(PawnKindDef standPawnKindDef,Pawn owner)
        {
            StandPawn standPawn = (StandPawn)PawnGenerator.GeneratePawn(standPawnKindDef, owner.Faction);
            #region 原版自带的Master系统
            //if (standPawn.training == null)//如果使用this.playerSettings.Master系统保存，这就是必要的
            //{
            //    standPawn.training = new Pawn_TrainingTracker(standPawn);
            //    standPawn.training.Train(TrainableDefOf.Obedience, owner, true);//训练并设置主人
            //}
            #endregion
            standPawn.health.AddHediff(StandHediffDefOf.SyncHediff);
            standPawn.owner = owner;
            standPawn.SetupField(standPawn.def.GetModExtension<StandExtension>());
            standPawn.standPower = owner.needs.TryGetNeed<Need_StandPower>();
            standPawn.standPower.Stand = standPawn;
            standPawn.standAbilityTracker = new StandAbilityTracker(standPawn);//OK，技能框架完成
            standPawn.InitStandAbility();

            if (!Find.WorldPawns.Contains(standPawn))//为了进行派系保存
            {
                Find.WorldPawns.PassToWorld(standPawn, RimWorld.Planet.PawnDiscardDecideMode.KeepForever);//这个函数会导致派系不对,重新设置派系
                standPawn.SetFaction(owner.Faction);
            }
            return standPawn;
        }

        //public static StandPawn CreateStandPawn(Pawn owner,IntVec3 position, Map map, Faction faction)
        //{ 
        //   return CreateStandPawn("StarPlatinum", owner, position, map, faction);
        //}


        //public static StandPawn CreateStandPawn(string pawnKindDefName, Pawn owner, IntVec3 position, Map map, Faction faction)
        //{
        //    //因为玩家可能所属不同类别，所以要这么获得派系？
        //    PawnKindDef kind = DefDatabase<PawnKindDef>.GetNamed(pawnKindDefName);
        //    PawnGenerationRequest request = new PawnGenerationRequest(kind, faction, PawnGenerationContext.NonPlayer, -1, true, true, false, false, false, false, 0f, false, false, false, false, false, false, false, false, 0, 0, null, 0, null, null, null, null, null, 0f, 0f, Gender.None);


        //    //standPawn.playerSettings = new Pawn_PlayerSettings(standPawn) {Master = owner };
        //    //standPawn.playerSettings.Master = owner;
        //    //standPawn.owner = owner;
        //    //IntVec3 spawnPos = position;
        //    Pawn stand = PawnGenerator.GeneratePawn(request);//生成

        //    StandPawn instanceStandPawn = (StandPawn)GenSpawn.Spawn(stand, position, map);
        //    instanceStandPawn.owner = owner;
        //    //instanceStandPawn.playerSettings.Master = owner;//这段错误的原因是pawn是一个机械根本不是一个棋子，所以不能生套已有的逻辑。


        //    return instanceStandPawn;
        //}
    }
}
